//
//  ScrollViewExtension.swift
//  MySwiftPullToRefresh
//
//  Created by likai on 2017/5/2.
//  Copyright © 2017年 terry. All rights reserved.
//

import UIKit

private var refreshHeaderViewKey: UInt8 = 0
private var refreshFooterViewKey: UInt8 = 0

public extension UIScrollView{
    
    private var spr_refreshHeaderView:RefreshHeaderView?{
        get {
            //http://blog.leichunfeng.com/blog/2015/06/26/objective-c-associated-objects-implementation-principle/#jtss-tsina
            //Objective-C Associated Objects 的实现原理
            
            return objc_getAssociatedObject(self, &refreshHeaderViewKey) as? RefreshHeaderView
        }
        set{
            
            objc_setAssociatedObject(self, &refreshHeaderViewKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
            
            newValue.map{insertSubview($0, at: 0)}//这里需要在研究一下
        }
    }
    
    public func spr_addIndicatorHeader(color: UIColor = Default.color,
                                       height: CGFloat = Default.short,
                                       action: @escaping () -> Void){
        
        spr_refreshHeaderView = IndicatorHeaderView(color: color, height: height, action: action)
        
        
        
        
        
    }
    
    public func spr_addTextHeader(loadingText:String = Default.loadingText,
                                  pullingText:String = Default.pullingText,
                                  releaseText:String = Default.releaseText,
                                  font:UIFont = Default.font,
                                  color:UIColor = Default.color,
                                  heigt:CGFloat = Default.short,action: @escaping () -> Void){
        
        spr_refreshHeaderView = TextHeaderView(loadingText: loadingText, pullingText: pullingText, releaseText: releaseText, font: font, color: color, height: heigt, action: action)
        
    }
    
    public func spr_addGIFHeader(data:Data,
                                 isBig: Bool,
                                 height:CGFloat,
                                 action: @escaping () -> Void){
        
        spr_refreshHeaderView = GIFHeaderView(data: data, isBig: isBig, height: height, action: action)
        
        
    }
    
    public func spr_addGIFTextHeader(data:Data,
                                     loadingText:String = Default.loadingText,
                                     pullingText:String = Default.pullingText,
                                     releaseText:String = Default.releaseText,
                                     font:UIFont = Default.font,
                                     color:UIColor = Default.color,
                                     heigt:CGFloat = Default.short,
                                     action: @escaping () -> Void){
        
        spr_refreshHeaderView = GIFTextHeaderView(data: data, loadingText: loadingText, pullingText: pullingText, releaseText: releaseText, font: font, color: color, height: heigt, action: action)
        
        
        
    }
    
    public func spr_addSuperCarHeader(action: @escaping () -> Void){
        spr_refreshHeaderView = SuperCatHeaderView(height: 120, action: action)
    }
    
    
    
    
    
    public func spr_beginRefreshing(){
        spr_refreshHeaderView?.beginRefreshing()
    }
    
    public func spr_endRefreshing(){
        spr_refreshHeaderView?.endRefreshing()
//        spr_refre
    }
}

struct Default {
    static let color: UIColor = UIColor.black.withAlphaComponent(0.8)
    static let font: UIFont = UIFont.systemFont(ofSize: 14)
    static let loadingText = "Loading..."
    static let pullingText = "Pull down to refresh"
    static let releaseText = "Release to refresh"
    static let pullingFooterText = "Pull up to load more"
    static let releaseFooterText = "Release to load more"
    static let high : CGFloat = 120
    static let short : CGFloat = 60
    
    
}
